﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web.Mvc;
using CoolCode;
using CoolCode.Web;
using Kangxin.Mvc.Models;
using System.Web.Security;

namespace Kangxin.Mvc.Controllers
{
    [HandleError]
    public class AdminController : CoolCode.Web.Mvc.ControllerBase
    {
        private IMembershipService _MemberService;
        public IMembershipService MemberService
        {
            get
            {
                if (_MemberService == null)
                {
                    _MemberService = Digger.Dig<IMembershipService>();
                }
                return _MemberService;
            }
        }

        private IMenuService _MenuService;
        public IMenuService MenuService
        {
            get
            {
                if (_MenuService == null)
                    _MenuService = Digger.Dig<IMenuService>();
                return _MenuService;
            }
        }

        public AdminController()
        {
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        public ActionResult Users()
        {
            Title="用户管理";
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchUsers()
        {
            var form = this.HttpContext.Request.Form;
            var users = MemberService.PagingUser(new PageParam());
            FlexiGridResult result = new FlexiGridResult();
            result.page = 1;
            result.total = users.TotalRecords;
            foreach (var user in users)
            {
                FlexiGridRow row = new FlexiGridRow { cell = new List<string>() };
                row.id = user.UserId.ToString();
                row.cell.AddRange(new string[]{user.UserId.ToString() , user.aspnet_User.UserName , user.Email, 
                    user.LastLoginDate.ToString(Config.DateFormat), 
                    user.CreateDate.ToString(Config.DateFormat),
                    user.aspnet_User.LastActivityDate.ToString(Config.DateFormat)
                });
                result.rows.Add(row);
            }
            return Json(result);
            //Test Data
            //string json="{\"page\":1,\"total\":87,\"rows\":[{\"id\":\"1\",\"cell\":[\"1\",\"苹果汁\",\"每箱24瓶\",\"18.0000\",\"39\",\"0\",\"1\"]},{\"id\":\"2\",\"cell\":[\"2\",\"牛奶\",\"每箱24瓶\",\"19.0000\",\"17\",\"40\",\"2\"]},{\"id\":\"3\",\"cell\":[\"3\",\"蕃茄酱\",\"每箱12瓶\",\"10.0000\",\"13\",\"70\",\"3\"]},{\"id\":\"4\",\"cell\":[\"4\",\"盐\",\"每箱12瓶\",\"22.0000\",\"53\",\"0\",\"4\"]},{\"id\":\"5\",\"cell\":[\"5\",\"麻油\",\"每箱12瓶\",\"21.3500\",\"0\",\"0\",\"5\"]},{\"id\":\"6\",\"cell\":[\"6\",\"酱油\",\"每箱12瓶\",\"25.0000\",\"120\",\"0\",\"6\"]},{\"id\":\"7\",\"cell\":[\"7\",\"海鲜粉\",\"每箱30盒\",\"30.0000\",\"15\",\"0\",\"7\"]},{\"id\":\"8\",\"cell\":[\"8\",\"胡椒粉\",\"每箱30盒\",\"40.0000\",\"6\",\"0\",\"8\"]},{\"id\":\"9\",\"cell\":[\"9\",\"鸡\",\"每袋500克\",\"97.0000\",\"29\",\"0\",\"9\"]},{\"id\":\"10\",\"cell\":[\"10\",\"蟹\",\"每袋500克\",\"31.0000\",\"31\",\"0\",\"10\"]},{\"id\":\"11\",\"cell\":[\"11\",\"大众奶酪\",\"每袋6包\",\"21.0000\",\"22\",\"30\",\"11\"]},{\"id\":\"12\",\"cell\":[\"12\",\"德国奶酪\",\"每箱12瓶\",\"38.0000\",\"86\",\"0\",\"12\"]},{\"id\":\"13\",\"cell\":[\"13\",\"龙虾\",\"每袋500克\",\"6.0000\",\"24\",\"0\",\"13\"]},{\"id\":\"14\",\"cell\":[\"14\",\"沙茶\",\"每箱12瓶\",\"23.2500\",\"35\",\"0\",\"14\"]},{\"id\":\"15\",\"cell\":[\"15\",\"味精\",\"每箱30盒\",\"15.5000\",\"39\",\"0\",\"15\"]},{\"id\":\"16\",\"cell\":[\"16\",\"饼干\",\"每箱30盒\",\"17.4500\",\"29\",\"0\",\"16\"]},{\"id\":\"17\",\"cell\":[\"17\",\"猪肉\",\"每袋500克\",\"39.0000\",\"0\",\"0\",\"17\"]},{\"id\":\"18\",\"cell\":[\"18\",\"墨鱼\",\"每袋500克\",\"62.5000\",\"42\",\"0\",\"18\"]},{\"id\":\"19\",\"cell\":[\"19\",\"糖果\",\"每箱30盒\",\"9.2000\",\"25\",\"0\",\"19\"]},{\"id\":\"20\",\"cell\":[\"20\",\"桂花糕\",\"每箱30盒\",\"81.0000\",\"40\",\"0\",\"20\"]},{\"id\":\"21\",\"cell\":[\"21\",\"花生\",\"每箱30包\",\"10.0000\",\"3\",\"40\",\"21\"]},{\"id\":\"22\",\"cell\":[\"22\",\"糯米\",\"每袋3公斤\",\"21.0000\",\"104\",\"0\",\"22\"]},{\"id\":\"23\",\"cell\":[\"23\",\"燕麦\",\"每袋3公斤\",\"9.0000\",\"61\",\"0\",\"23\"]},{\"id\":\"24\",\"cell\":[\"24\",\"汽水\",\"每箱12瓶\",\"4.5000\",\"20\",\"0\",\"24\"]},{\"id\":\"25\",\"cell\":[\"25\",\"巧克力\",\"每箱30盒\",\"14.0000\",\"76\",\"0\",\"25\"]}]}";
            //return Content(json, "application/json");
        }

        public ActionResult UserList(int? page)
        {
            var list = MemberService.PagingUser(new PageParam( page.GetValueOrDefault() ));
            return PartialView(list);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult UserDelete(Guid id)
        {
            var k = id;
            return this.Message(true);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult UserEdit(Guid id)
        {
            var user = Membership.GetUser(id);
            if (user == null)
                return Content("用户名不存在或者用户已经被删除！Error key: " + id);
            string key = user.UserName;
            string[] roles =Roles.GetAllRoles();
            string[] selectedRoles = Roles.GetRolesForUser(key);
            var list = this.MenuService.ListMenuInUser(key);
            var filterRoles= roles.Select(c=> new SelectListItem { Selected =selectedRoles.Contains(c), Value=c ,Text =c}).ToList();
            //SelectList roleList=new SelectList( filterRoles ,"Text","Text", selectedRoles);
            UserRoleModel model = new UserRoleModel
            {
                UserName = key,
                Roles = filterRoles,// roleList.ToList<SelectListItem>(),
                //SelectedRoles = Roles.GetRolesForUser(key)
                 UserRoles = list 
            };
            return PartialView(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveUser()
        {
            var form = this.HttpContext.Request.Form;
            UserRoleModel model = new UserRoleModel();
            if (TryUpdateModel(model))
            {
                string key = model.UserName;

                return Content("保存成功!");
            }
            else
            {
                return Content("提交数据有误!");
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchRoles()
        {
            var form = this.HttpContext.Request.Form;
            string key = form["UserName"];
            if (string.IsNullOrEmpty(key))
                key = "bruce";
            var list = this.MenuService.ListMenuInUser(key);
            FlexiGridResult result = new FlexiGridResult();
            result.page = 1;
            result.total = list.Count ;
            foreach (var item in list)
            {
                FlexiGridRow row = new FlexiGridRow { cell = new List<string>() };
                row.id = item.MenuID.ToString();
                row.cell.AddRange(new string[]{ item.MenuID.ToString() , item.BelongTo   , item.MenuDesc , 
                    item.CanRead.ToString().ToLower(), 
                    item.CanWrite.ToString().ToLower(),
                    item.CanDelete.ToString().ToLower(),
                    item.CanExport.ToString().ToLower()
                });
                result.rows.Add(row);
            }
            return Json(result);
            //Test Data
            //string json="{\"page\":1,\"total\":87,\"rows\":[{\"id\":\"1\",\"cell\":[\"1\",\"苹果汁\",\"每箱24瓶\",\"18.0000\",\"39\",\"0\",\"1\"]},{\"id\":\"2\",\"cell\":[\"2\",\"牛奶\",\"每箱24瓶\",\"19.0000\",\"17\",\"40\",\"2\"]},{\"id\":\"3\",\"cell\":[\"3\",\"蕃茄酱\",\"每箱12瓶\",\"10.0000\",\"13\",\"70\",\"3\"]},{\"id\":\"4\",\"cell\":[\"4\",\"盐\",\"每箱12瓶\",\"22.0000\",\"53\",\"0\",\"4\"]},{\"id\":\"5\",\"cell\":[\"5\",\"麻油\",\"每箱12瓶\",\"21.3500\",\"0\",\"0\",\"5\"]},{\"id\":\"6\",\"cell\":[\"6\",\"酱油\",\"每箱12瓶\",\"25.0000\",\"120\",\"0\",\"6\"]},{\"id\":\"7\",\"cell\":[\"7\",\"海鲜粉\",\"每箱30盒\",\"30.0000\",\"15\",\"0\",\"7\"]},{\"id\":\"8\",\"cell\":[\"8\",\"胡椒粉\",\"每箱30盒\",\"40.0000\",\"6\",\"0\",\"8\"]},{\"id\":\"9\",\"cell\":[\"9\",\"鸡\",\"每袋500克\",\"97.0000\",\"29\",\"0\",\"9\"]},{\"id\":\"10\",\"cell\":[\"10\",\"蟹\",\"每袋500克\",\"31.0000\",\"31\",\"0\",\"10\"]},{\"id\":\"11\",\"cell\":[\"11\",\"大众奶酪\",\"每袋6包\",\"21.0000\",\"22\",\"30\",\"11\"]},{\"id\":\"12\",\"cell\":[\"12\",\"德国奶酪\",\"每箱12瓶\",\"38.0000\",\"86\",\"0\",\"12\"]},{\"id\":\"13\",\"cell\":[\"13\",\"龙虾\",\"每袋500克\",\"6.0000\",\"24\",\"0\",\"13\"]},{\"id\":\"14\",\"cell\":[\"14\",\"沙茶\",\"每箱12瓶\",\"23.2500\",\"35\",\"0\",\"14\"]},{\"id\":\"15\",\"cell\":[\"15\",\"味精\",\"每箱30盒\",\"15.5000\",\"39\",\"0\",\"15\"]},{\"id\":\"16\",\"cell\":[\"16\",\"饼干\",\"每箱30盒\",\"17.4500\",\"29\",\"0\",\"16\"]},{\"id\":\"17\",\"cell\":[\"17\",\"猪肉\",\"每袋500克\",\"39.0000\",\"0\",\"0\",\"17\"]},{\"id\":\"18\",\"cell\":[\"18\",\"墨鱼\",\"每袋500克\",\"62.5000\",\"42\",\"0\",\"18\"]},{\"id\":\"19\",\"cell\":[\"19\",\"糖果\",\"每箱30盒\",\"9.2000\",\"25\",\"0\",\"19\"]},{\"id\":\"20\",\"cell\":[\"20\",\"桂花糕\",\"每箱30盒\",\"81.0000\",\"40\",\"0\",\"20\"]},{\"id\":\"21\",\"cell\":[\"21\",\"花生\",\"每箱30包\",\"10.0000\",\"3\",\"40\",\"21\"]},{\"id\":\"22\",\"cell\":[\"22\",\"糯米\",\"每袋3公斤\",\"21.0000\",\"104\",\"0\",\"22\"]},{\"id\":\"23\",\"cell\":[\"23\",\"燕麦\",\"每袋3公斤\",\"9.0000\",\"61\",\"0\",\"23\"]},{\"id\":\"24\",\"cell\":[\"24\",\"汽水\",\"每箱12瓶\",\"4.5000\",\"20\",\"0\",\"24\"]},{\"id\":\"25\",\"cell\":[\"25\",\"巧克力\",\"每箱30盒\",\"14.0000\",\"76\",\"0\",\"25\"]}]}";
            //return Content(json, "application/json");
        }
    }
}
